package de.staxx6.bomber;

import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import com.jme3.input.InputManager;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.math.ColorRGBA;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;

public class MenuState extends AbstractAppState {

	private ViewPort viewPort;
	private Node rootNode;
	private Node guiNode;
	@SuppressWarnings("unused")
	private AssetManager assetManager;
	private AppStateManager stateManager;
	private InputManager inputManger;
	private Node localRootNode = new Node("Start Screen RootNode");
	private Node localGuiNode = new Node("Start Screen GuiNode");
	
	public MenuState(SimpleApplication app) {
		this.rootNode = app.getRootNode();
		this.viewPort = app.getViewPort();
		this.guiNode = app.getGuiNode();
		this.assetManager = app.getAssetManager();
		this.inputManger = app.getInputManager();
		this.stateManager = app.getStateManager();
	}
	
	public void initialize(AppStateManager stateManager, Application app) {
		super.initialize(stateManager, app);
		viewPort.setBackgroundColor(ColorRGBA.Blue);
		
		inputManger.addMapping("GameState", new KeyTrigger(KeyInput.KEY_SPACE));
		inputManger.addListener(actionListener, new String[] { "GameState" });
		
		System.out.println("MenuState!");
	}
	
	private ActionListener actionListener = new ActionListener() {
		@Override
		public void onAction(String name, boolean keyPressed, float delta) {
			if(name.equals("GameState") && !keyPressed) {
				stateManager.detach(Bomber.MENUSTATE);
				stateManager.attach(Bomber.GAMESTATE);
			}
		}
	};
	
	public void update(float delta) {
		
	}
	
	public void stateAttached(AppStateManager stateManger) {
		rootNode.attachChild(localRootNode);
		guiNode.attachChild(localGuiNode);
	}
	
	public void stateDetached(AppStateManager stateManager) {
		rootNode.detachChild(localRootNode);
		guiNode.detachChild(localGuiNode);
	}
}
